package com.example.mybatisplus.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.Entity.UserEntity;
import com.example.mybatisplus.Mapper.UserMapper;
import com.example.mybatisplus.Pojo.Result;
import com.example.mybatisplus.Service.UserService;
import com.example.mybatisplus.model.SearchModel;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/test_mybatis_plus")
public class UserController {

    @Resource(name = "userServiceImpl")//写实现类的小写名
    private UserService userService;

    @Resource(name = "userMapper")//切记一定要注入Bean
    private UserMapper userMapper;


    //用userService的实现类调用mapper中自定义的方法findAll()查找所有用户信息
    @GetMapping("/users")
    public @ResponseBody List<UserEntity> getAllUser() {
        //@ResponseBody将返回值转换为 JSON 格式，并将其作为 HTTP 响应体返回给客户端。
        return userService.findAll();
    }

    //用接口userMapper中继承的BaseMapper接口中的方法查询 指定用户的信息
    @GetMapping(value = "selectByName/{name}")
    public @ResponseBody List<UserEntity> selectByName(@PathVariable("name") String name) {
        //PathVariable:用于将请求参数映射到方法参数或类属性上。它通常用于处理 URL 路径参数
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);// 设置等值查询，即查询对应名字的UserEntity对象
        return userMapper.selectList(queryWrapper);
    }


    // 模拟复杂分页查询
    @GetMapping("pageByAge/{age}")
    public IPage<UserEntity> page(@PathVariable Integer age) {
        Page<UserEntity> page = new Page<>(1, 2);
        IPage<UserEntity> userEntityIPage = userService.selectUserByAge(page, age);
        userEntityIPage.getRecords().forEach(System.out::println);
        return userService.selectUserByAge(page, age);
    }

    // 简单分页查询
    @GetMapping("/page")
    public List<UserEntity> paging(@RequestBody SearchModel<UserEntity> search) {
        IPage<UserEntity> searchResult = userService.selectAll(search);
        searchResult.getRecords().forEach(System.out::println);
        System.out.println("总页数：" + searchResult.getPages());
        System.out.println("当前页：" + searchResult.getCurrent());
        System.out.println("当前页显示条数：" + searchResult.getTotal());
        System.out.println("每页最多显示条数：" + searchResult.getSize());
        System.out.println("当前页所有数据：" + searchResult.getRecords());
        return searchResult.getRecords();
    }


    @PostMapping("/insert")
    public Result<UserEntity> insertUser(@RequestBody UserEntity user) {
        //@RequestBody表示将请求体中的数据映射到User对象中
        return userService.insertUser(user);
    }

    @DeleteMapping("/delete/{id}")
    public Result<UserEntity> deleteUserById(@PathVariable Integer id) {
        return userService.deleteUser(id);
    }

    @PutMapping("/update")
    public Result<UserEntity> updateUser(@RequestBody UserEntity user) {
        return userService.updateUser(user);
    }


    @GetMapping("/selectOneById/{id}")
    public Result<UserEntity> selectOneUser(@PathVariable Integer id) {
        return userService.selectOne(id);
    }

    @GetMapping("/ChooseAll")//不能用浏览器请求
    public Result<UserEntity> ChooseAllUser(@RequestBody SearchModel<UserEntity> search) {
        // @RequestBody表示将请求体中的数据映射到SearchModel<UserEntity>对象中
        Result<UserEntity> data = userService.ChooseAll(search);
        System.out.println(data.getCount()); //2
        System.out.println(data.getStatusCode());  //2000
        System.out.println(data.getMessage());  //查询成功
        return data;
    }
}
